﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace dnFileEncryptor
{
	/// <summary>
	/// Class used to tag enums with additional values
	/// </summary>
	[AttributeUsage(AttributeTargets.Field, AllowMultiple = true)]
	public class KeyValueAttribute : Attribute
	{
		public string Key;
		public object Value;

		public KeyValueAttribute(string key, object value)
		{
			Key = key;
			Value = value;
		}
	}

	/// <summary>
	/// Hash calculation algorithm. Used for <see cref="CDC.NCHS.DHANES.Framework.Utils.Text.VerifyHash"/> and <see cref="CDC.NCHS.DHANES.Framework.Utils.Text.ComputeHash"/>
	/// </summary>
	public enum HashAlgorithmType
	{
		MD5, SHA1, SHA256, SHA384, SHA512, SHA1Managed
	}

	public enum PasswordStrength
	{
		Low,
		Medium,
		High
	}

	/// <summary>
	/// Hash calculation algorithm. Used for <see cref="CDC.NCHS.DHANES.Framework.Utils.Text.VerifyHash"/> and <see cref="CDC.NCHS.DHANES.Framework.Utils.Text.ComputeHash"/>
	/// </summary>
	public enum EncryptionAlgorithmType
	{
		[KeyValue("key-size", 64)]
		[KeyValue("block-size", 64)]
		DES,
		[KeyValue("key-size", 128)]
		[KeyValue("block-size", 64)]
		TDES,
		[KeyValue("key-size", 256)]
		[KeyValue("block-size", 128)]
		AES
	}

	public class EnumValues
	{
		/// <summary>
		/// Returns value of an attribute by key
		/// </summary>
		/// <param name="en">Enum value</param>
		/// <returns>String or value.ToString()</returns>
		public static T GetEnumValue<T>(Enum value, string key)
		{
			Type type = value.GetType();

			MemberInfo[] memInfo = type.GetMember(value.ToString());

			if (memInfo != null && memInfo.Length > 0)
			{
				object[] attrs = memInfo[0].GetCustomAttributes(typeof(KeyValueAttribute),
				false);
				foreach (object attr in attrs)
				{
					if (((KeyValueAttribute)attr).Key == key)
						return (T)((KeyValueAttribute)attr).Value;
				}
			}

			return default(T);
		}

		public static T ParseEnum<T>(string value, T defaultValue)
		{
			if (string.IsNullOrEmpty(value))
				return defaultValue;

			T result = defaultValue;
			try
			{
				result = (T)Enum.Parse(defaultValue.GetType(), value);
			}
			catch { }

			return result;
		}
	}
}
